/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.util 
 */

package com.apache.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.apache.cache.util.Validator;

/**  
 * description:  获取配置信息工具类
 * @author dwu 创建时间：2014-7-23         
 */
@Deprecated
public class ConfigUtil {

	private Map<String, Long> lastUpateTime = new HashMap<String, Long>();//最后修改时间
	private Map<String, Map<String, String>> constant = new HashMap<String, Map<String, String>>();//存放系统配置参数
	private static ConfigUtil instance;
	private String filePath = "";

	private ConfigUtil() {
		filePath = Validator.getClassLoaderPath() + "config/";
	}

	public static ConfigUtil getInstance() {
		if (null == instance) {
			instance = new ConfigUtil();
		}
		return instance;
	}

	/**
	  * description: 加载配置文件
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private String initConfig(String key, String fileName) {
		File oldfile = new File(filePath + fileName);
		long lastModletime = oldfile.lastModified();
		Long lastTime = lastUpateTime.get(fileName);
		if (null == lastTime || lastTime < lastModletime) {//有更新从新加载
			lastUpateTime.put(fileName, lastModletime);
			Properties prop = getProperties(filePath + fileName);
			if (Validator.isEmpty(prop))
				return "";
			Map<String, String> map = new HashMap<String, String>((Map) prop);
			Set propertySet = map.entrySet();
			Map<String, String> param = new HashMap<String, String>();
			for (Object o : propertySet) {
				Map.Entry entry = (Map.Entry) o;
				param.put(entry.getKey().toString(), entry.getValue().toString());
			}
			constant.put(fileName, param);
		}
		if (Validator.isNull(key)) {
			return "";
		} else {
			if (Validator.isEmpty(constant.get(fileName)))
				return "";
			return Validator.getDefaultStr(constant.get(fileName).get(key), "");
		}
	}

	/**
	 * 获取property文件的属性值
	 * @param key
	 * @return
	 */
	public String getValueByKey(String propFileName, String key) {
		return initConfig(key, propFileName);
	}

	public String getValueByKey(String propFileName, String key, String charset) {
		try {
			charset = Validator.getDefaultStr(charset, "UTF-8");
			String result = new String(initConfig(key, propFileName).getBytes("iso8859-1"), charset);
			return result;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取某个配置文件下的所有配置信息
	 * @param propFilePath
	 * @return
	 */
	public Map<String, String> getAllProperty(String propFileName) {
		initConfig("", propFileName);
		return constant.get(propFileName);
	}

	/**
	 * 获取Poperties文件
	 * @param filePath 资源文件路径
	 * @return         返回Poperties中文件属性   
	 */
	public Properties getProperties(String filePath) {
		Properties prop = null;
		FileInputStream fileInput = null;
		try {
			File file = new File(filePath);
			if (file.exists()) {// 判断文件夹是否存在
				prop = new Properties();
				fileInput = new FileInputStream(filePath);
				prop.load(fileInput);
			}
		} catch (IOException e) {
			System.out.println("文件读取失败 key=" + e.getMessage());
		} finally {
			try {
				if (null != prop)
					fileInput.close();
			} catch (Exception e) {
				System.out.println("文件流关闭失败 key=" + e.getMessage());
			}
		}
		return prop;
	}

}
